﻿<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("initValues");
_spBodyOnLoadFunctionNames.push("addEvents");


function initValues(){
	ChangeEventService()    
    
    var textboxlisttype = getTagFromIdentifierAndTitle("input","TextField","List type");
    //get the selected value

    var listtype = '';

    var controlsource = getTagFromIdentifierAndTitle("input","TextField","Source");
    if(textboxlisttype.value == "Document Library"){
      
      controlsource.parentNode.parentNode.parentNode.style.display="none";
    }
    else{
      
      controlsource.parentNode.parentNode.parentNode.style.display="";        	  
    }

}

function addEvents() {

//get list name control and add onchange event
//add an onchange event to the dropdown
getTagFromIdentifierAndTitle("select","DropDownChoice","List name").onchange = function() {ChangeEventListName()};
getTagFromIdentifierAndTitle("select","DropDownChoice","Service").onchange = function() {ChangeEventService()};

}

function ChangeEventService(){

    //get the mapping field
    var mapping = getTagFromIdentifierAndTitle("Textarea","TextField","Mapping");

    //alert(mapping);
    //get the service dropdown
    var ddlservice = getTagFromIdentifierAndTitle("select","DropDownChoice","Service");
    //get the selected value
    var selectedService = ddlservice.options[ddlservice.selectedIndex].text;
    if(selectedService.indexOf("Entities") == -1)
    {
        mapping.parentNode.parentNode.parentNode.style.display="none";
    }
    else
    {
        mapping.parentNode.parentNode.parentNode.style.display="";
    }
} 

function ChangeEventListName() {
	var context = new SP.ClientContext.get_current();
	this.website = context.get_web();
	var ddllistname = getTagFromIdentifierAndTitle("select","DropDownChoice","List name");
	var selectedlistname = ddllistname.options[ddllistname.selectedIndex].text;
	this.list = context.get_web().get_lists().getByTitle(selectedlistname);
    context.load(list);
    //var controlsource1 = getTagFromIdentifierAndTitle("select","DropDownChoice","Source");
//    controlsource1.options.length = 0;
//    var fields = list.get_fields();
//    context.load(fields);
    context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    //getFields(fields, controlsource1);
}

//function getFields(fields, controlsource){
//    //var controlsource = getTagFromIdentifierAndTitle("select","DropDownChoice","Source");
//	var fldEnumerator = fields.getEnumerator();
//    while(fldEnumerator.moveNext())
//    {
//      var currentfield = fldEnumerator.get_current();         
//      if(!(currentfield.get_hidden()) && !(currentfield.get_readOnlyField())){
//      var opt = document.createElement("option");
//      controlsource.options.add(opt);
//      opt.text = currentfield.get_title();
//      opt.value = currentfield.get_title();
//      }
//   	}
//}

function onQuerySucceeded(sender, args) {   	
    var textboxlisttype = getTagFromIdentifierAndTitle("input","TextField","List type");
    //get the selected value

    var listtype = '';
	listtype = list.get_baseType();
    var controlsource = getTagFromIdentifierAndTitle("input","TextField","Source");
    if(listtype == '1'){
      textboxlisttype.value = "Document Library";
      controlsource.parentNode.parentNode.parentNode.style.display="none";
    }
    else{
      textboxlisttype.value = "List";
      controlsource.parentNode.parentNode.parentNode.style.display="";          	  
    }
}

function onQueryFailed(sender, args) {
   alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}


//this gets the field based on title identifier and tagname
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
 var len = identifier.length;
 var tags = document.getElementsByTagName(tagName);
 for (var i=0; i < tags.length; i++) {
 var tempString = tags[i].id;
 if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
  return tags[i];
 }
 }
 return null;
}</script>
